/*
 * Copyright (C) 2010 Cyril Mottier (http://www.cyrilmottier.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.greendroid;

import java.util.List;

import com.qingshui.browser.R;

import android.content.Context;
import android.graphics.Rect;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

/**
 * A {@link QuickActionList} is an implementation of a {@link QuickActionWidget}
 * that displays {@link QuickAction}s in a grid manner. This is usually used to create
 * a shortcut to jump between different type of information on screen.
 *
 * @author Benjamin Fellous
 * @author Cyril Mottier
 */
public class QuickActionList extends QuickActionWidget {

	private ListView mListView;

	public QuickActionList(Context context) {
		super(context);

		setContentView(R.layout.gd_quick_action_list);

		final View v = getContentView();
		mListView = (ListView) v.findViewById(R.id.gdQuickActionList);
		
	}


	@Override
	protected void populateQuickActions(final List<QuickAction> quickActions) {
		BaseAdapter adapter= new BaseAdapter() {

			public View getView(int position, View view, ViewGroup parent) {

				LinearLayout listView = (LinearLayout) view;

				if (view == null) {
					final LayoutInflater inflater = LayoutInflater.from(getContext());
					listView= (LinearLayout) inflater.inflate(R.layout.gd_quick_action_list_item, mListView, false);
				}

				QuickAction quickAction = quickActions.get(position);
				TextView text = (TextView)listView.findViewById(R.id.TabText);
				ImageView image=(ImageView)listView.findViewById(R.id.TabImage);
				image.setImageDrawable(quickAction.mDrawable);
				
				text.setText(quickAction.mTitle);
				//textView.setCompoundDrawablesWithIntrinsicBounds(null, quickAction.mDrawable, null, null);
				
				return listView;

			}

			public long getItemId(int position) {
				return position;
			}

			public Object getItem(int position) {
				return null;
			}

			public int getCount() {
				return quickActions.size();
			}
		};
		mListView.setAdapter(adapter);
		adapter.notifyDataSetChanged();
		mListView.setOnItemClickListener(mInternalItemClickListener);
		
	}

	@Override
	protected void onMeasureAndLayout(Rect anchorRect, View contentView) {

		contentView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		contentView.measure(MeasureSpec.makeMeasureSpec(getScreenWidth(), MeasureSpec.EXACTLY),
				LayoutParams.WRAP_CONTENT//MeasureSpec.makeMeasureSpec(getScreenHeight()/2, MeasureSpec.EXACTLY)
				);
		
		int rootHeight =contentView.getMeasuredHeight();
		int offsetY = getArrowOffsetY();
		int dyTop = anchorRect.top;
		int dyBottom = getScreenHeight() - anchorRect.bottom;
		Log.d("anchor:",""+anchorRect.toShortString());
		boolean onTop = (dyTop > dyBottom);
		Log.d("anchor:","top "+anchorRect.top+"bottom "+anchorRect.bottom);
		
		int popupY = (onTop) ? anchorRect.top - rootHeight + offsetY : anchorRect.bottom - offsetY;
		Log.d("popupY:",""+popupY+" h:"+rootHeight);
		
		setWidgetSpecs(popupY, onTop);
	}

	private OnItemClickListener mInternalItemClickListener = new OnItemClickListener() {
		public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
			getOnQuickActionClickListener().onQuickActionClicked(QuickActionList.this, position);
			if (getDismissOnClick()) {
				dismiss();
			}
		}
	};

}

